<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>

    <script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/commons.js"></script>
    <script>
        $(function () {
            //获取数据
            getDicValueList();

            //给新增按钮绑定单击事件
            $("#createDicValueBtn").on("click", function () {
                window.location.href = "settings/dictionary/value/saveDicValuePage.do"
            });

            //给更新按钮绑定单击事件
            $("#editDicValueBtn").on("click", function () {
                var checkedList = $("#tBody input:checked")
                if (checkedList.size() !== 1) {
                    alert("一次只能更新一条数据");
                    return;
                } else {
                    var idCode = $.trim(checkedList.val());
                    let split = idCode.split("-");
                    let id = split[0];
                    alert(id);
                    window.location.href = "settings/dictionary/value/editDicValuePage.do?id=" + id;
                }
            });

            //给删除按钮绑定单击事件
            $("#deleteDicValueBtn").on("click",function () {
                //获取选中的input标签
                var checkedList = $("#tBody input:checked")
                //判断选中的标签是否多于一个
                if (checkedList.length === 0){
                    alert("至少删除一个！");
                    return;
                }
                if (!confirm("您确定要删除这" + checkedList.length + "条数据?")){
                    alert("你已经去取消删除!");
                    return;
                }
                //拼接id变量
                var idStr = "";
                $.each(checkedList,function (index,item) {
                    idStr += "idCode=" + $.trim(item.value) + "&";
                })
                //删掉最后一个&符号
                idStr = idStr.substring(0,idStr.length - 1);
                //发送ajax请求删除
                $.ajax({
                    url:"settings/dictionary/value/deleteDicValue.do",
                    data:idStr,
                    type:"get",
                    success:function (data) {
                        if (data.code === 1){
                            alert("您已经成功删除了" + data.data + "条数据");
                            //刷线页面
                            window.location.href = "settings/dictionary/value/index.do"
                        }else{
                            alert(data.message);
                        }
                    }
                });
            })


        })

        /**
         * 获取字典值数据
         */
        function getDicValueList() {
            $.ajax({
                url: "settings/dictionary/value/getDicValueList.do",
                type: "get",
                success: function (data) {
                    //需要拼接的字符串
                    var htmlStr = "";
                    $.each(data, function (index, item) {
                        if (index % 2 === 0) {
                            htmlStr += "<tr class=\"active\">"
                        } else {
                            htmlStr += "<tr>"
                        }
                        htmlStr += "<td><input type=\"checkbox\" value=\" " + item.id+"-"+ item.typeCode + "\" /></td>";
                        htmlStr += "<td>" + item.id + "</td>";
                        htmlStr += "<td>" + item.value + "</td>";
                        htmlStr += "<td>" + item.text + "</td>";
                        htmlStr += "<td>" + item.orderNo + "</td>";
                        htmlStr += "<td>" + item.typeCode + "</td>";
                        htmlStr += "</tr>";
                    })
                    $("#tBody").html(htmlStr)
                }
            })
        }
    </script>
</head>
<body>

<div>
    <div style="position: relative; left: 30px; top: -10px;">
        <div class="page-header">
            <h3>字典值列表</h3>
        </div>
    </div>
</div>
<div class="btn-toolbar" role="toolbar" style="background-color: #F7F7F7; height: 50px; position: relative;left: 30px;">
    <div class="btn-group" style="position: relative; top: 18%;">
        <button id="createDicValueBtn" type="button" class="btn btn-primary"><span
                class="glyphicon glyphicon-plus"></span> 创建
        </button>
        <button id="editDicValueBtn" type="button" class="btn btn-default"><span
                class="glyphicon glyphicon-edit"></span> 编辑
        </button>
        <button id="deleteDicValueBtn" type="button" class="btn btn-danger"><span
                class="glyphicon glyphicon-minus"></span> 删除
        </button>
    </div>
</div>
<div style="position: relative; left: 30px; top: 20px;">
    <table class="table table-hover">
        <thead>
        <tr style="color: #B3B3B3;">
            <td><input type="checkbox" id="chkedAll"/></td>
            <td>序号</td>
            <td>字典值</td>
            <td>文本</td>
            <td>排序号</td>
            <td>字典类型编码</td>
        </tr>
        </thead>
        <tbody id="tBody">

        </tbody>
    </table>
</div>

</body>
</html>